//
//  XMLParser.m
//  MyOWNUITableView
//
//  Created by Jordy Heemskerk on 18-05-10.
//  Copyright 2010 ITopia. All rights reserved.
//

#import "XMLParser.h"


@implementation XMLParser

-(XMLParser *) initXMLParser {
	[super init];
	
	appDelegate = [[UIApplication sharedApplication] delegate];
	
	return self;
}

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
	if (!parseType) {
		parseType = [[NSString alloc] init];
	}
	if ([elementName isEqualToString:@"product"]) {
		parseType = @"products";
	} else if ([elementName isEqualToString:@"category"]) {
		parseType = @"categories";
	}
	
	if ([parseType isEqualToString:@"products"]) {
	
		if ([elementName isEqualToString:@"product"]) {
			aProduct = [[Product alloc] init];
		}
		
	}
	
	if ([parseType isEqualToString:@"categories"]) {
		
		if ([elementName isEqualToString:@"category"]) {
			aCategory = [[Category alloc] init];
			aCategory.subCategories = [[NSMutableArray alloc] init];
		} else {
			aSubCategory = [[SubCategory alloc] init];
			[aSubCategory setSId: [NSNumber numberWithInt:[elementName intValue]]];
		}
	}
	
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	
	if (!currentEllementValue)
		currentEllementValue = [[NSMutableString alloc] initWithString:string];
	else
		[currentEllementValue appendString:string];

	
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	
	[currentEllementValue setString:[currentEllementValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
	
	if ([parseType isEqualToString:@"products"]) {

		if ([elementName isEqualToString:@"product"]) {
			NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://localhost/~Jordy/shopdeluxe/image.php"]];
			[aProduct setImage:[UIImage imageWithData:data]];
			[appDelegate.products addObject:aProduct];
		} else {
			[aProduct setValue:currentEllementValue forKey:elementName];
		}
		
	}
	
	if ([parseType isEqualToString:@"categories"]) {
		
		if ([elementName isEqualToString:@"category"]) {
			[aCategory setName:currentEllementValue];
			[appDelegate.categories	addObject:aCategory];
		} else {
			NSArray *splitElements = [currentEllementValue componentsSeparatedByString:@":"];
			[aSubCategory setSId:[splitElements objectAtIndex:0]];
			[aSubCategory setValue:[splitElements objectAtIndex:1]];
			[aSubCategory setName:elementName];
			[[aCategory subCategories] addObject:aSubCategory];
		}

		
	}
	
	[currentEllementValue release];
	currentEllementValue = nil;

	if ([elementName isEqualToString:@"product"] || [elementName isEqualToString:@"category"]) {
		[parseType release];
		parseType = nil;
	}
	
	
	
}

- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
	NSLog(@"Error : %@", [parseError localizedDescription]);

}

@end
